package com.runshu.newsmartark.dbutil;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.runshu.newsmartark.base.MyApplication;
import com.runshu.newsmartark.entity.CategoryListEntity;
import com.runshu.newsmartark.entity.ConfirmStockEntity;
import com.runshu.newsmartark.entity.ProductsBean;
import com.runshu.newsmartark.entity.UserInfoEntity;
import com.runshu.newsmartark.util.common.LogUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Demo class
 *
 * @author yu
 * @date 12/9/20
 * @decs：
 */

public class DBUtils {
    private static DBUtils instance;
    //操作数据库
    SmartRoomDatabase database = SmartRoomDatabase.getInstance(MyApplication.getApplication());
    UserDao userDao = database.userDao();
    ProAllDao proAllDao = database.proAllDao();
    private int size = 999;

    public static DBUtils getInstance() {
        if (instance == null) {
            synchronized (DBUtils.class) {
                if (instance == null) {
                    instance = new DBUtils();
                }
            }
        }
        return instance;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public ProAllDao getProAllDao() {
        return proAllDao;
    }

    public List<ProductsBean> getproByType(String category, String modle, String manufacturer) {
        return proAllDao.getProByType(category, modle, manufacturer);
    }

    public List<ProAllEntity> getProBySerialNos(String... serialNos) {
        if (serialNos.length == 0) return new ArrayList<>();
        List<ProAllEntity> result = new ArrayList<>();
        for (int i = 0; i <= (serialNos.length / size); i++) {
            if ((i * size + serialNos.length % size) == serialNos.length) {
                result.addAll(proAllDao.getProBySerialNos(Arrays.copyOfRange(serialNos, i * size, serialNos.length)));
            } else
                result.addAll(proAllDao.getProBySerialNos(Arrays.copyOfRange(serialNos, i * size, i * size + size)));
        }
        return result;
    }

    public List<ProSelfEntity> getProBySerialNosToSelfEntity(String... serialNos) {
        if (serialNos.length == 0) return new ArrayList<>();
        List<ProSelfEntity> result = new ArrayList<>();
        for (int i = 0; i <= (serialNos.length / size); i++) {
            if ((i * size + serialNos.length % size) == serialNos.length) {
                result.addAll(proAllDao.getProBySerialNosToSelfEntity(Arrays.copyOfRange(serialNos, i * size, serialNos.length)));
            } else
                result.addAll(proAllDao.getProBySerialNosToSelfEntity(Arrays.copyOfRange(serialNos, i * size, i * size + size)));
        }
        return result;

    }

    ;

    //查询本柜中拿出的数据 todo ok
    public List<ConfirmStockEntity.AllProductsBean> getMenusPros(String... serialNos) {
//        if (serialNos.length == 0) return new ArrayList<>();
        ArrayList<MenTmpTableEntity> menTmpTableEntities = MenTmpTableEntity.generateList(serialNos);
        proAllDao.deleteMen();
        proAllDao.insertAllMen(menTmpTableEntities);
        return proAllDao.getMenusProsSQ();
    }

    //查询本柜中增加的数据  todo ok
    public List<ConfirmStockEntity.AllProductsBean> getAddPros(String... serialNos) {

        if (serialNos.length == 0) return new ArrayList<>();
        Map<String, ConfirmStockEntity.AllProductsBean> result = new HashMap<>();
        for (int i = 0; i <= (serialNos.length / size); i++) {
            List<ConfirmStockEntity.AllProductsBean> resultL = new ArrayList<>();
            if ((i * size + serialNos.length % size) == serialNos.length) {
                resultL = proAllDao.getAddPros(Arrays.copyOfRange(serialNos, i * size, serialNos.length));

            } else
                resultL = proAllDao.getAddPros(Arrays.copyOfRange(serialNos, i * size, i * size + size));
            for (ConfirmStockEntity.AllProductsBean tmp : resultL) {
                String a = tmp.getProductNo() + tmp.getProductModel() + tmp.getProductName();
                if (result.containsKey(a)) {
                    result.get(a).setStockChangeNum(result.get(a).getStockChangeNum() + tmp.getStockChangeNum());
                } else {
                    result.put(a, tmp);
                }
            }
        }

        return new ArrayList<>(result.values());
    }


    //    获取上架的商品集合 todo ok
    public List<ProAllEntity> getAddProAllList(String... serialNos) {

        if (serialNos.length == 0) return new ArrayList<>();
        List<ProAllEntity> result = new ArrayList<>();
        for (int i = 0; i <= (serialNos.length / size); i++) {
            if ((i * size + serialNos.length % size) == serialNos.length) {
                result.addAll(proAllDao.getAddProAllList(Arrays.copyOfRange(serialNos, i * size, serialNos.length)));
            } else
                result.addAll(proAllDao.getAddProAllList(Arrays.copyOfRange(serialNos, i * size, i * size + size)));
        }


        return result;

    }

    //    获取下架的商品集合 todo ok
    public List<ProAllEntity> getMenusProAllList(String... serialNos) {

//        if (serialNos.length == 0) {
//            return new ArrayList<>();
//        }
        List<ProAllEntity> result = new ArrayList<>();
        for (int i = 0; i <= (serialNos.length / size); i++) {
            List<ProAllEntity> menusProAllList;
            if ((i * size + serialNos.length % size) == serialNos.length) {
                menusProAllList = proAllDao.getMenusProAllList(Arrays.copyOfRange(serialNos, i * size, serialNos.length));
//                result.retainAll(menusProAllList);
            } else {
                menusProAllList = proAllDao.getMenusProAllList(Arrays.copyOfRange(serialNos, i * size, i * size + size));
//                result.retainAll(menusProAllList);
            }
            if (result.size() == 0) {
                result.addAll(menusProAllList);
            } else
                result.retainAll(menusProAllList);
        }


        return result;

    }

    //获取用户全部数据
    public List<UserInfoEntity> getUserList() {
        return userDao.getUserList();
    }


    //返回匹配卡片 用户信息
    public UserInfoEntity getUserByIdCardNo(String cardID) {
        return userDao.getUserByIdCardNo(cardID);
    }

    //返回匹配姓名 用户信息
    public UserInfoEntity getUserByUserName(String userName) {
        return userDao.getUserByUserName(userName);
    }

    //插入用户集合
    public void insertUserList(List<UserInfoEntity> userList) {

        userDao.addUserInfo(userList);

    }
    //插入单体用户数据

    public void insertUserInfo(UserInfoEntity userInfoEntity) {
        userDao.addUserInfo(userInfoEntity);
    }

    //删除用户表数据
    public void deleteAllUserInfo() {
        userDao.deleteAllUserInfo();

    }


    /**
     * 查询最后一条数据的更新时间
     *
     * @return
     */

    String getLastUpdateTime() {
        return proAllDao.getLastUpdateTime();

    }

    //插入医院下所有商品集合
    public void insertAllProList(List<ProAllEntity> proAllList) {

        proAllDao.insertAllPro(proAllList);

    }

    //插入医院下所有商品集合
    public synchronized void insertSelfProList(List<ProAllEntity> proAllList) {
        Gson gson = new Gson();
        List<ProSelfEntity> list = new CopyOnWriteArrayList<>();
        list = gson.fromJson(gson.toJson(proAllList), new TypeToken<List<ProSelfEntity>>() {
        }.getType());

        proAllDao.deleteSelfPro();
        proAllDao.insertSelfPro(list);

    }

    //清空资源
    public void release() {
        if (database != null) {
            database.close();
            database.release();
            database = null;
            instance = null;
        }
    }


}